define(['durandal/composition', 'perfectScrollbar','jquery'], function(composition, perfectScrollbar,$) {
	composition.addBindingHandler('hasFocus');
	//用法<textarea data-bind="psTextarea:{width:270,height:100},textInput:val"></textarea>
	//可以通过val获取值
	composition.addBindingHandler('psTextarea', {
		init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
			var vals = valueAccessor();
			var $wrapper = $('<div class="ps-textarea-wrapper"></div>');
			var $area = $(element);
			var $parent = $area.parent();
			$area.addClass('ps-textarea-textarea');
			$wrapper.append($area);
			$parent.append($wrapper);

			var width = vals.width;
			var height = vals.height;
			//set css
			if(width) {
				$wrapper.css('width', width + 'px');
			}
			if(height) {
				$wrapper.css('height', height + 'px');
			}
			debugger;
			$area.css({
				width: $wrapper.width() + 'px' //会去除border的宽度
			});
			var area = $area.get(0);
			allBindings().textInput.subscribe(function(newVal) {
				area.style.height = 'auto'; //用于在下面获取准确的scrollHeight;
				area.style.height = area.scrollHeight + 'px';
				$wrapper.scrollTop(area.scrollHeight - height);
				$wrapper.perfectScrollbar('update');

			});
			$wrapper.perfectScrollbar();
		}
	});
})